package com.linksoul.service;

import com.linksoul.domain.MbtiOption;
import com.linksoul.domain.MbtiQuestion;
import com.linksoul.domain.TestDimensionDetail;
import com.linksoul.domain.dto.OptionDTO;
import com.linksoul.domain.dto.QuestionDTO;
import com.linksoul.enums.ResultCode;
import com.linksoul.exception.MbtiException;
import com.linksoul.mapper.MbtiQuestionMapper;
import com.linksoul.mapper.MbtiRequestLogMapper;
import com.linksoul.mapper.TestDimensionDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2025/05/01/12:23
 **/
@Service
@RequiredArgsConstructor
public class MbtiService {
    private static final Set<String> VALID_DIMENSIONS = new HashSet<>(Arrays.asList("EI", "SN", "TF", "JP"));
    @Autowired
    private final MbtiQuestionMapper questionMapper;
    @Autowired
    private final MbtiRequestLogMapper testDimensionDetailMapper;

    // 获取测试问题集
    public List<QuestionDTO> getTestQuestions() {
        List<MbtiQuestion> questions = questionMapper.selectQuestionsByDimension(null);
        if (questions == null || questions.isEmpty()) {
            throw new MbtiException(ResultCode.QUESTION_NOT_FOUND);
        }

        return questions.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    // 按维度获取问题
    public List<QuestionDTO> getQuestionsByDimension(String dimension) {
        if (!VALID_DIMENSIONS.contains(dimension)) {
            throw new MbtiException(ResultCode.INVALID_DIMENSION);
        }

        List<MbtiQuestion> questions = questionMapper.selectQuestionsByDimension(dimension);
        if (questions == null || questions.isEmpty()) {
            throw new MbtiException(ResultCode.QUESTION_NOT_FOUND);
        }

        return questions.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    private QuestionDTO convertToDTO(MbtiQuestion question) {
        QuestionDTO dto = new QuestionDTO();
        dto.setQuestionId(question.getQuestionId());
        dto.setDimension(question.getDimension());
        dto.setQuestionText(question.getQuestionText());

        List<MbtiOption> options = questionMapper.selectOptionsByQuestionId(question.getQuestionId());
        dto.setOptions(convertOptionsToDTO(options));

        return dto;
    }

    private List<OptionDTO> convertOptionsToDTO(List<MbtiOption> options) {
        if (options == null) {
            return Collections.emptyList();
        }
        return options.stream().map(opt -> {
            OptionDTO optDto = new OptionDTO();
            if(opt!=null) {
                optDto.setOptionId(opt.getOptionId());
                optDto.setOptionText(opt.getOptionText());
            }
            return optDto;
        }).collect(Collectors.toList());
    }

    /**
     * 根据requestId查询维度得分详情
     */
    public TestDimensionDetail getDimensionDetail(String requestId) {
        System.out.println(testDimensionDetailMapper.getDimensionDetail(requestId));
        return testDimensionDetailMapper.getDimensionDetail(requestId);


    }
}